package com.ihunanren.common.spider.test;

import static com.google.common.base.Strings.isNullOrEmpty;

import java.io.PrintWriter;
import java.util.Set;

import com.ihunanren.common.spider.AbstractWebSipder;
import com.ihunanren.common.spider.WebPage;
import com.ihunanren.common.spider.WebSpiderParam;


/**
 * @author kalman03
 */
public class MyWeb {

//	private final static String linkRegex = "[http://][^\\s\"\'\\?]*(" + domain + ")[^\\s\"\'>]*";

	private final static int thread = 10;
	private final static int deep = 2;
	private final static String baseUrl = "http://www.baidu.com";
	private final static String regex = "";
	public static void main(String[] args) {
		
		WebSpiderParam param = createWebSpiderParam(thread, deep, baseUrl, regex);
		PrintWriter out  = null;
		new AbstractWebSipder<PrintWriter>(param,out) {
			@Override
			public void onStart(WebSpiderParam param, PrintWriter out) {
				System.out.println(param.toString());
			}
			@Override
			public void doHandle(WebPage webPage, PrintWriter rout) {
				System.out.println("doHandle");
				String parentUrl = webPage.getCurUrl();
				Set<String> childList = webPage.getAllLinkList();
				for(String childUrl : childList){
					System.out.println(parentUrl+"------------>"+childUrl);
				}
			}
			@Override
			public void onException(Throwable e, PrintWriter out) {
				System.out.println("onException");
			}
			@Override
			public void onEnd(long success, long failed, PrintWriter out) {
				System.out.println("end");
			}
		}.doSpider();

	}

	private static WebSpiderParam createWebSpiderParam(int thread,int deep,String baseUrl,String regex){
		WebSpiderParam param = new WebSpiderParam();
		param.setBaseUrl(baseUrl);
		param.setDeep(deep);
		if(!isNullOrEmpty(regex)){
			param.setLinkRegex(new String[]{regex});
		}
		param.setThread(thread);
		return param;
	}
}
